﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class hardcodingArticle : BasePage
    {
        WTODataContext ctx = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetArticleCategory();
                InitArticleContent();
            }
        }

        private void InitArticleContent()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int aid = int.Parse(Request.QueryString["id"]);
                hardcoding_article art = ctx.hardcoding_articles.FirstOrDefault(p => p.ID == aid);
                if (art != null)
                {
                    this.editor1.Text = art.content;
                }
            }
        }

        private void GetArticleCategory()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
               this.litCategory.Text = Utility.GetDescByIDFromXML(Server.MapPath("~/metadate/articlekind.xml"), Request.QueryString["id"]);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            bool createMode = true;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int aid = int.Parse(Request.QueryString["id"]);
                hardcoding_article art = ctx.hardcoding_articles.FirstOrDefault(p => p.ID == aid);
                if (art != null)
                {
                    createMode = false;
                }
                else
                {
                    art = new hardcoding_article();
                    art.ID = aid;
                }
                art.content = this.editor1.Text;

                if (createMode)
                {
                    ctx.hardcoding_articles.InsertOnSubmit(art);
                }
                ctx.SubmitChanges();
                
            }
        }
    }
}